***********************************************
**************** ISSUANCES DATA ***************
***********************************************
/*
In this do file, we import and prepare the data on
issuances.
*/

*----------------
* PATHS
*----------------
global replication `c(pwd)'
global data "$replication\Data"
global inter "$replication\Dta - Intermediate"
global final "$replication\Dta - Final"
global graphs "$replication\Graphs"

clear all
use "$inter\AuctionsData.dta", clear
drop if sec_bnpid==.

*================
* HOUSEKEEPING
*================
* Dropping variables we don't need
drop new_isin
drop isin_indic
drop isin_freq
drop iss_mn
drop iss_dy
drop vin_yr
drop vin_mn
drop vin_dy

* Creating maturity groups
gen matgroup=rmon2mat 
replace matgroup=600 if(matgroup>410)
replace matgroup=360 if(matgroup>211&matgroup<=410)
replace matgroup=180 if(matgroup>=145&matgroup<=210)
replace matgroup=60 if(matgroup>=45&matgroup<=74)
replace matgroup=120 if(matgroup>=75&matgroup<=144)
replace matgroup=36 if(matgroup>=21&matgroup<=44)
replace matgroup=3 if(matgroup>=0&matgroup<=4)
replace matgroup=6 if(matgroup>=5&matgroup<=7)
replace matgroup=9 if(matgroup>=8&matgroup<=10)
replace matgroup=12 if(matgroup>=11&matgroup<=14)
replace matgroup=18 if(matgroup>=15&matgroup<=20)

save "$final\AuctionsData.dta", replace

*=======================
* YEARLY AGGREGATION
*=======================
collapse (sum) totalot_ngdp, by(matgroup iss_yr)
label var totalot_ngdp "Issuance as a fraction of GDP"
replace totalot_ngdp=0 if(totalot_ngdp==.)
 
reshape wide totalot_ngdp, i(iss_yr) j(matgroup)
tsset iss_yr
format iss_yr %ty //not sure

replace totalot_ngdp3=0 if(totalot_ngdp3==.)
replace totalot_ngdp6=0 if(totalot_ngdp6==.)
replace totalot_ngdp9=0 if(totalot_ngdp9==.)
replace totalot_ngdp12=0 if(totalot_ngdp12==.)
replace totalot_ngdp18=0 if(totalot_ngdp18==.)
replace totalot_ngdp36=0 if(totalot_ngdp36==.)
replace totalot_ngdp60=0 if(totalot_ngdp60==.)
replace totalot_ngdp120=0 if(totalot_ngdp120==.)
replace totalot_ngdp180=0 if(totalot_ngdp180==.)
replace totalot_ngdp360=0 if(totalot_ngdp360==.)
replace totalot_ngdp600=0 if(totalot_ngdp600==.)

* Constructing weights of issuances for each maturity
gen totmalot_ngdp= totalot_ngdp3+totalot_ngdp6+totalot_ngdp9+totalot_ngdp12+totalot_ngdp18+totalot_ngdp36+totalot_ngdp60+totalot_ngdp120+totalot_ngdp180+totalot_ngdp360+totalot_ngdp600
gen issw_3=totalot_ngdp3/totmalot_ngdp
gen issw_6=totalot_ngdp6/totmalot_ngdp
gen issw_9=totalot_ngdp9/totmalot_ngdp
gen issw_12=totalot_ngdp12/totmalot_ngdp
gen issw_18=totalot_ngdp18/totmalot_ngdp
gen issw_36=totalot_ngdp36/totmalot_ngdp
gen issw_60=totalot_ngdp60/totmalot_ngdp
gen issw_120=totalot_ngdp120/totmalot_ngdp
gen issw_180=totalot_ngdp180/totmalot_ngdp
gen issw_360=totalot_ngdp360/totmalot_ngdp
gen issw_600=totalot_ngdp600/totmalot_ngdp

* Average maturity
gen avmon_mat=issw_3*3+issw_6*6+issw_9*9+issw_12*12+issw_18*18+issw_36*36+issw_60*60+issw_120*120+issw_180*180+issw_360*360+totalot_ngdp600*600

* Saving the yearly information
rename iss_yr year	
drop if year==.

save "$final\AuctionsYearlyAggregation.dta", replace

*=======================
* QUARTERLY AGGREGATION
*=======================
clear all
use "$final\AuctionsData.dta", clear
drop if sec_bnpid==.
 
collapse (sum) totalot_ngdp, by(matgroup iss_qy)
label var totalot_ngdp "Issuance as a fraction of GDP"
replace totalot_ngdp=0 if(totalot_ngdp==.)
 
reshape wide totalot_ngdp, i(iss_qy) j(matgroup)
tsset iss_qy
format iss_qy %tq

replace totalot_ngdp3=0 if(totalot_ngdp3==.)
replace totalot_ngdp6=0 if(totalot_ngdp6==.)
replace totalot_ngdp9=0 if(totalot_ngdp9==.)
replace totalot_ngdp12=0 if(totalot_ngdp12==.)
replace totalot_ngdp18=0 if(totalot_ngdp18==.)
replace totalot_ngdp36=0 if(totalot_ngdp36==.)
replace totalot_ngdp60=0 if(totalot_ngdp60==.)
replace totalot_ngdp120=0 if(totalot_ngdp120==.)
replace totalot_ngdp180=0 if(totalot_ngdp180==.)
replace totalot_ngdp360=0 if(totalot_ngdp360==.)
replace totalot_ngdp600=0 if(totalot_ngdp600==.)

* Constructing weights of issuances for each maturity
gen totmalot_ngdp= totalot_ngdp3+totalot_ngdp6+totalot_ngdp9+totalot_ngdp12+totalot_ngdp18+totalot_ngdp36+totalot_ngdp60+totalot_ngdp120+totalot_ngdp180+totalot_ngdp360+totalot_ngdp600
gen issw_3=totalot_ngdp3/totmalot_ngdp
gen issw_6=totalot_ngdp6/totmalot_ngdp
gen issw_9=totalot_ngdp9/totmalot_ngdp
gen issw_12=totalot_ngdp12/totmalot_ngdp
gen issw_18=totalot_ngdp18/totmalot_ngdp
gen issw_36=totalot_ngdp36/totmalot_ngdp
gen issw_60=totalot_ngdp60/totmalot_ngdp
gen issw_120=totalot_ngdp120/totmalot_ngdp
gen issw_180=totalot_ngdp180/totmalot_ngdp
gen issw_360=totalot_ngdp360/totmalot_ngdp
gen issw_600=totalot_ngdp600/totmalot_ngdp

* Average maturity
gen avmon_mat=issw_3*3+issw_6*6+issw_9*9+issw_12*12+issw_18*18+issw_36*36+issw_60*60+issw_120*120+issw_180*180+issw_360*360+totalot_ngdp600*600

* Saving the quarterly information
rename iss_qy qy	
drop if qy==.

save "$final\AuctionsQuarterlyAggregation.dta", replace

*=======================
* MONTHLY AGGREGATION
*=======================
clear all
use "$final\AuctionsData.dta", clear
drop if sec_bnpid==.

collapse (sum) totalot_ngdp, by(matgroup iss_my)
replace totalot_ngdp=0 if(totalot_ngdp==.) 

reshape wide totalot_ngdp, i(iss_my) j(matgroup)
tsset iss_my
format iss_my %tm
rename iss_my my

replace totalot_ngdp3=0 if(totalot_ngdp3==.)
replace totalot_ngdp6=0 if(totalot_ngdp6==.)
replace totalot_ngdp9=0 if(totalot_ngdp9==.)
replace totalot_ngdp12=0 if(totalot_ngdp12==.)
replace totalot_ngdp18=0 if(totalot_ngdp18==.)
replace totalot_ngdp36=0 if(totalot_ngdp36==.)
replace totalot_ngdp60=0 if(totalot_ngdp60==.)
replace totalot_ngdp120=0 if(totalot_ngdp120==.)
replace totalot_ngdp180=0 if(totalot_ngdp180==.)
replace totalot_ngdp360=0 if(totalot_ngdp360==.)
replace totalot_ngdp600=0 if(totalot_ngdp600==.)

* Construct Average Maturity of Issuance
gen totmalot_ngdp= totalot_ngdp3+totalot_ngdp6+totalot_ngdp9+totalot_ngdp12+totalot_ngdp18+totalot_ngdp36+totalot_ngdp60+totalot_ngdp120+totalot_ngdp180+totalot_ngdp360
gen issw_3=totalot_ngdp3/totmalot_ngdp
gen issw_6=totalot_ngdp6/totmalot_ngdp
gen issw_9=totalot_ngdp9/totmalot_ngdp
gen issw_12=totalot_ngdp12/totmalot_ngdp
gen issw_18=totalot_ngdp18/totmalot_ngdp
gen issw_36=totalot_ngdp36/totmalot_ngdp
gen issw_60=totalot_ngdp60/totmalot_ngdp
gen issw_120=totalot_ngdp120/totmalot_ngdp
gen issw_180=totalot_ngdp180/totmalot_ngdp
gen issw_360=totalot_ngdp360/totmalot_ngdp
gen issw_600=totalot_ngdp600/totmalot_ngdp

* Average maturity
gen avmon_mat=issw_3*3+issw_6*6+issw_9*9+issw_12*12+issw_18*18+issw_36*36+issw_60*60+issw_120*120+issw_180*180+issw_360*360

* Saving the monthly information
save "$final\AuctionsMonthlyAggregation.dta", replace
